package com.assess.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.assess.web.model.Assessment;
import com.assess.web.model.RandomData;
import com.assess.web.service.*;
import com.assess.web.util.DateUtil;
import com.assess.web.util.RespParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/assessment")
public class AssessmentController extends BaseController {
    @Autowired
    private AssessmentService assessmentService;
    @Autowired
    private CompanyService companyService;
    @Autowired
    private AddressService addressService;
    @Autowired
    private SchemeService schemeService;
    @Autowired
    private PhasesService phasesService;
    @Autowired
    private RandomDataService randomDataService;

    /**
     * 跳转评估列表
     *
     * @return
     */
    @RequestMapping("list")
    public ModelAndView list() {
        ModelAndView view = new ModelAndView("/assessment/list");
        view.addObject("companyList", companyService.listCompany());
        return view;
    }

    @ResponseBody
    @RequestMapping("page")
    public JSONObject pageAssessment(@RequestParam(name = "companyId", required = false) Integer companyId,
                                     @RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
                                     @RequestParam(name = "pageSize", defaultValue = "15") Integer pageSize) {
        return assessmentService.page(companyId, pageNum, pageSize);
    }

    @RequestMapping("detail")
    public ModelAndView detail(@RequestParam("assessmentId") Integer assessmentId) {
        Assessment assessment = assessmentService.getAssessment(assessmentId);
        ModelAndView view = new ModelAndView("/assessment/detail");
        view.addObject("list", randomDataService.list(assessmentId));
        view.addObject("companyName", companyService.getCompany(assessment.getCompanyId()).getName());
        view.addObject("assessmentTime", DateUtil.format(assessment.getAssessmentTime()));
        view.addObject("address", addressService.getAddress(assessment.getAddressId()).getName());
        return view;
    }

    /**
     * 跳转到抽选设置
     *
     * @return
     */
    @RequestMapping("")
    public ModelAndView index(@RequestParam("assessmentId") Integer assessmentId) {
        Assessment assessment = assessmentService.getAssessment(assessmentId);

        ModelAndView view = new ModelAndView("/selection/rule");
        view.addObject("vo", assessment);
        view.addObject("schemeList", schemeService.listScheme(assessment.getSchemeId()));
        view.addObject("phasesList", phasesService.list(0));
        view.addObject("companyName", companyService.getCompany(assessment.getCompanyId()).getName());
        view.addObject("addressName", addressService.getAddress(assessment.getAddressId()).getName());
        view.addObject("assessmentTime", DateUtil.format(assessment.getAssessmentTime()));
        return view;
    }


    /**
     * 保存评估表
     *
     * @param companyId
     * @param companyName
     * @param assessmentTime
     * @param schemeId
     * @param formatId
     * @param addressTendersName
     * @param addressTendersId
     * @return
     */
    @ResponseBody
    @RequestMapping("save")
    public RespParam<Integer> save(@RequestParam(name = "companyId", required = false) Integer companyId,
                                   @RequestParam(name = "companyName") String companyName,
                                   @RequestParam(name = "assessmentTime") String assessmentTime,
                                   @RequestParam(name = "schemeId") Integer schemeId,
                                   @RequestParam(name = "formatId") Integer formatId,
                                   @RequestParam(name = "addressTendersName") String addressTendersName,
                                   @RequestParam(name = "addressTendersId", required = false) Integer addressTendersId) {
        if (null == companyId) {
            companyId = companyService.saveCompany(companyName.trim());
        }

        if (null == addressTendersId) {
            addressTendersId = addressService.saveAddress(companyId, addressTendersName.trim());
        }
        Assessment assessment = new Assessment();
        assessment.setAddressId(addressTendersId);
        assessment.setCompanyId(companyId);
        assessment.setSchemeId(schemeId);
        assessment.setPasesId(formatId);
        assessment.setAssessmentTime(DateUtil.parse(assessmentTime));
        assessmentService.save(assessment);
        return success(assessment.getId());
    }

    @ResponseBody
    @RequestMapping("update")
    public void updateAssessment(@RequestParam("assessmentId") Integer assessmentId,
                                 @RequestParam("rule") String rule) {
        Assessment assessment = new Assessment();
        assessment.setRule(rule);
        assessment.setId(assessmentId);
        assessmentService.update(assessment);
    }
}
